//The purpose of this class is to show the user their exercise history on the date chosen.
package com.mckillion.icare;

//Import the java classes required
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.mckillion.icare.R;

//The class extends activity which allows the Android features to be imported.
public class ExerciseDiary extends Activity{
	//Declare the variables required for this class.
	//private variables are used to promote consistency.
	private ImageButton backButton, homeButton, settingsButton;
	private TextView targetText, takenText, dateText;
	private ImageView infoButton;
	private TextView infoText;
	private Bundle extra;
	private String date, target, taken;
	private File exFile;
	private FileInputStream exis;
	private BufferedReader exReader;
	
	//overwrite the onCreate method so that variables are instantiated on opening.
	public void onCreate(Bundle savedInstanceState) {
        //Call to the super class.
		super.onCreate(savedInstanceState);
        
		//Remove title and notification bars to give it an android feel.
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
		//Set the content layout to the main_screen XML file.
        setContentView(R.layout.exercise_diary_layout);
        
        //Use an intent to find the current date and convert it to a string.
        extra = getIntent().getExtras();
        if (extra != null) {
            date = extra.getString("date");
        }
        
    	//make the objects match the objects in the layout file.
        dateText = (TextView) findViewById(R.id.textView3);
        targetText = (TextView) findViewById(R.id.textView5);
        takenText = (TextView) findViewById(R.id.textView7);
        
        //show the date in the appropriate text view.
        dateText.setText(date);
        
        try 
        {	//Attempt to open and read the exercise file.
        	exFile = new File("/mnt/sdcard/Download/" + date + "exercise.txt");
			exis = new FileInputStream(exFile);
			exReader = new BufferedReader(new InputStreamReader(exis));
			target = "";		taken = "";
			
			//If the file values exist put them into the text views. 
			if ((target = exReader.readLine()) != null && (taken = exReader.readLine()) != null)
			{	targetText.setText(target);
				takenText.setText(taken);
			}	
			
			//close the file reader.
			exReader.close();
			
        } 	catch (Exception e) {
			//If the file does not exists do nothing as default values are shown.
		}
        
    	//make the objects match the objects in the layout file.
        backButton = (ImageButton) findViewById(R.id.back_button);
        homeButton = (ImageButton) findViewById(R.id.home_button);
        settingsButton = (ImageButton) findViewById(R.id.settings_button);
        infoButton=(ImageView)findViewById(R.id.imageView4);
        
        //Set a listener for the information button
        infoButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//Use a layout inflater to open a pop-up window with the informational text.
				LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
				View popupView = layoutInflater.inflate(R.layout.info_popup, null);  
			    final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,  LayoutParams.WRAP_CONTENT);
			    
			    infoText = (TextView) popupWindow.getContentView().findViewById(R.id.info_text);
			    infoText.setText("This is the exercise history for the chosen day." + "\n" + "\n" +
			    					"If the historical data shows 'Data Not Entered' then the information is unavailable" +
			    					" for the given date." + "\n" + "\n" +
			    					"Press dismiss to close this message.");
			    
			    //create a dismiss button to close the pop-up window.
			    Button dismiss = (Button)popupView.findViewById(R.id.dismiss_button);
	            dismiss.setOnClickListener(new Button.OnClickListener(){
	            	@Override
	            	public void onClick(View v) {
	            		popupWindow.dismiss();
	            	}});
	               
	            //Put the pop-up window in the centre of the screen
	            popupWindow.showAtLocation(infoButton, Gravity.CENTER, 0, 0);
	   }});
        
        //Set a listener for the home button which starts the HomeScreen class
        homeButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(ExerciseDiary.this, HomeScreen.class);
				startActivity(i);
			}
		});
        
        //Set a listener for the back button which starts the ExerciseHistory class
        backButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//Show feedback to the user as there is a delay.
				Context context = getApplicationContext();
	   		    CharSequence text = "Please Wait...";
			    int duration = Toast.LENGTH_LONG;
			    Toast toast = Toast.makeText(context, text, duration);
			    toast.show();
			    
			    //Open the IntakeHistory class.
				Intent i = new Intent(ExerciseDiary.this, ExerciseHistory.class);
				startActivity(i);
			}
		});
        
        //Set a listener for the settings button which starts the Settings class
        settingsButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(ExerciseDiary.this, Settings.class);
				startActivity(i);
			}
		});
        
	}
}

